//
//  ShareManager.m
//  PrintPort
//
//  Created by hy002 on 2017/12/26.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "ShareManager.h"
#import "AppDelegate.h"

@implementation ShareManager
+ (id)shareToManager
{
    static dispatch_once_t once_t;
    static ShareManager *manager = nil;
    dispatch_once(&once_t, ^{
        
        manager = [[ShareManager alloc]init];
    });
    manager.blackView.hidden = NO;
    return manager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _blackView.backgroundColor = RGBA(0, 0, 0, 0.6);
        [sharedDelegate.window addSubview:_blackView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bottomViewPressed)];
        [_blackView addGestureRecognizer:tap];
        UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-140*displaywidth-SCREER_HeiX, SCREEN_WIDTH, 140*displaywidth + SCREER_HeiX)];
        whiteView.backgroundColor = [UIColor whiteColor];
        [_blackView addSubview:whiteView];
        NSArray *imgArray = @[@"wxfx",@"wxpyq"];
        for (int i = 0; i < imgArray.count; i++)
        {
            UIButton *shareBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH/2-75*displaywidth+90*displaywidth*i, 25*displaywidth, 60*displaywidth, 60*displaywidth) ImageName:imgArray[i] Target:self Action:@selector(shareAction:) Title:nil Font:12.0];
            shareBtn.tag = 60+i;
            [whiteView addSubview:shareBtn];
        }
        UILabel *tostLab = [ZCControl createLabelWithFrame:CGRectMake(0, 105*displaywidth, SCREEN_WIDTH, 25*displaywidth) Font:12.0 Text:@"分享到第三方平台" Color:RGB(158, 158, 158)];
        tostLab.textAlignment = NSTextAlignmentCenter;
        [whiteView addSubview:tostLab];
    }
    _blackView.hidden = NO;
    return self;
    
}

- (void)bottomViewPressed
{
    _blackView.hidden = YES;

}

- (void)shareAction:(UIButton *)btn
{
    _blackView.hidden = YES;
    if (btn.tag == 60)
    {
        [self shareToType:WXSceneSession Title:_title Description:_descriptionStr ImageUrl:_imgUrl WebpageUrl:_webUrl];
    }
    else
    {
        [self shareToType:WXSceneTimeline Title:_title Description:_descriptionStr ImageUrl:_imgUrl WebpageUrl:_webUrl];
    }
}


- (void)shareToType:(int)scene Title:(NSString *)title Description:(NSString *)description ImageUrl:(NSString *)imgUrl WebpageUrl:(NSString *)webUrl
{
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = title;
    message.description = description;
    if (imgUrl.length>0) {
        [message setThumbImage:IMAGE(imgUrl)];
    } else {
        [message setThumbImage:[UIImage imageNamed:@"mofang20"]];
    }
    WXWebpageObject *webpageObject = [WXWebpageObject object];
    webpageObject.webpageUrl = webUrl;
    message.mediaObject = webpageObject;
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = scene;
    [WXApi sendReq:req];
}
@end
